package com.java1412.service.impl;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.java1412.dto.UserDto;
import com.java1412.entity.User;
import com.java1412.mapper.UserMapper;
import com.java1412.service.UserService;
import com.java1412.utils.PageUtil;
import com.java1412.utils.ResponseUtil;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author java1412
 * @since 2023-08-07 09:27:19
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public ResponseUtil findByUsername(String username, String password) {
        ResponseUtil r = null;
        // 1.用户名不存在
        // 2.用户名存在，但是密码不正确
        // 3.用户名存在，且密码正确，但是角色不正确
        // 4.登录成功
        User user = userMapper.findByUsername(username);
        if (user == null) {
            r = ResponseUtil.fail("用户名不存在");
        } else if (!user.getPassword().equals(password)) {
            r = ResponseUtil.fail("用户名存在，但是密码不正确");
        } else if (user.getRole().intValue() != 1) {
            r = ResponseUtil.fail("用户名存在，且密码正确，但是角色不正确");
        } else {
            user.setPassword(null);
            r = ResponseUtil.ok("登录成功", user);
        }
        return r;
    }

    @Override
    public ResponseUtil adminFindByUsername(String username, String password) {
        ResponseUtil r = null;
        User user = userMapper.findByUsername(username);
        if (null == user) {
            r = ResponseUtil.fail("用户名不存在");
        } else if (!user.getPassword().equals(password)) {
            r = ResponseUtil.fail("用户名存在，但是密码不正确");
        } else if (user.getRole().intValue() != 0) {
            r = ResponseUtil.fail("用户名存在，且密码正确，但是不是管理员");
        } else {
            user.setPassword(null);
            r = ResponseUtil.ok("认证成功", user);
        }
        return r;
    }

    @Override
    public ResponseUtil find(String username, Integer currentPage, Integer pageSize) {
        // 1.封装参数
        // 根据第几页计算出从第几条开始查询
        Integer pageIndexNo = (currentPage - 1) * pageSize;
        Map<String, Object> map = new HashMap();
        map.put("username", username);
        map.put("pageIndexNo", pageIndexNo);
        map.put("pageSize", pageSize);
        // 2.查询数据量
        Integer count = userMapper.findCount(map);
        // 3.查询需要的数据
        List<User> userList = userMapper.find(map);
        // 4.将查询到数据合并
        PageUtil<User> pageUtil = new PageUtil<>(currentPage, pageSize, count, userList);
        return ResponseUtil.ok("查询用户信息成功", pageUtil);
    }

    @Override
    public ResponseUtil add(UserDto userDto) {
        ResponseUtil r = null;
        // 设置页面没有提交数据
        userDto.setEmail("110@qq.com");
        userDto.setQuestion("你喜欢的女明星?");
        userDto.setAnswer("我喜欢林志玲");
        userDto.setRole(1);
        userDto.setCreateTime(new Date());
        userDto.setUpdateTime(new Date());
        Integer result = userMapper.add(userDto);
        if (result.intValue() > 0) {
            r = ResponseUtil.ok("注册用户成功");
        } else {
            r = ResponseUtil.fail("注册用户失败");
        }
        return r;
    }
}
